package bb.lanxing.device.search

import android.annotation.SuppressLint
import android.bluetooth.BluetoothAdapter
import android.content.BroadcastReceiver
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.IntentFilter
import androidx.activity.viewModels
import androidx.recyclerview.widget.DefaultItemAnimator
import androidx.recyclerview.widget.LinearLayoutManager
import bb.lanxing.R
import bb.lanxing.databinding.ActivitySearchDeviceBinding
import bb.lanxing.databinding.DataBindingAdapters
import bb.lanxing.device.sgdevcie.utils.SearchDeviceAdapter
import bb.lanxing.device.sgdevcie.utils.SearchDeviceAdapter.Companion.defaultInstance
import bb.lanxing.device.sgdevcie.xpair.uimodel.BleDescriptionDevice
import bb.lanxing.device.sgdevcie.xpair.viewmodel.SearchDeviceViewModel
import bb.lanxing.lib.devices.api.SmartDevice
import bb.lanxing.mvvm.base.BaseDBActivity
import bb.lanxing.view.BiciAlertDialogBuilder
import kotlin.jvm.internal.Intrinsics

class SearchDeviceActivity : BaseDBActivity<ActivitySearchDeviceBinding>() {
    private var adapter: SearchDeviceAdapter
    var isAddNew: Boolean = false
    var isResetBTAdapter: Boolean = false
    private val viewModel by viewModels<SearchDeviceViewModel>()


    override fun getLayoutId(): Int {
        return R.layout.activity_search_device
    }

    init {
        this.adapter = defaultInstance()
        adapter.setOnItemClickListener { baseQuickAdapter, view, i2 ->
        }
    }

    override fun initView(binding: ActivitySearchDeviceBinding) {
        setupActionBar(true)
        this.isAddNew = intent.getBooleanExtra(EXTRA_ADD_NEW, false)
        binding.recyclerView.adapter = adapter
        binding.recyclerView.layoutManager = LinearLayoutManager(this.context)
        binding.recyclerView.itemAnimator = DefaultItemAnimator()
        adapter.searchDeviceType = intent.getIntExtra("EXTRA_DEVICE_TYPE", -1)
        setTitle(DataBindingAdapters.getDeviceNameResource(adapter.searchDeviceType))
        viewModel.scanCallBack = { result ->
            if (result) {

            }

        }
    }

    override fun onResume() {
        super.onResume()
        viewModel.startScan()
        initBluetoothAdapterStateReceiver()
    }

    override fun onPause() {
        super.onPause()
        viewModel.stopScan()
        adapter.clear()
    }

    private fun initBluetoothAdapterStateReceiver() {
        registerReceiver(object : BroadcastReceiver() {
            @SuppressLint("MissingPermission")
            override fun onReceive(context: Context, intent: Intent) {
                val action = intent.action
                if (action == null || !Intrinsics.areEqual(
                        action, BluetoothAdapter.ACTION_STATE_CHANGED
                    )
                ) {
                    return
                }
                val intExtra =
                    intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.STATE_OFF)
                if (intExtra != BluetoothAdapter.STATE_OFF) {
                    if (intExtra == BluetoothAdapter.STATE_ON && this@SearchDeviceActivity.isResetBTAdapter && BluetoothAdapter.getDefaultAdapter().isEnabled) {
                        viewModel.startScan()
                    }
                    return
                }
                BluetoothAdapter.getDefaultAdapter().enable()
                this@SearchDeviceActivity.isResetBTAdapter = true
            }
        }, IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED))
    }

    private fun gotoUpdateSprintFirmware(str: String, z: Boolean) {
//        Intent intent = new Intent(this, SprintFirmwareUpdateActivity.class);
//        intent.putExtra(SprintFirmwareUpdateActivity.EXTRA_IS_OFFLINE_MODE, true);
//        intent.putExtra("EXTRA_DEVICE_ADDRESS", str);
//        intent.putExtra(SprintFirmwareUpdateActivity.EXTRA_IS_XOSS, z);
//        startActivity(intent);
        finish()
    }

    companion object {
        const val EXTRA_ADD_NEW: String = "add_new_device"
    }
}
